package com.ggh.jinjilive.ui.mall.goods.affirm;

import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;

import com.ggh.jinjilive.mobile.CommonMobile;
import com.ggh.jinjilive.mobile.Pay.WeChatPayMobile;
import com.ggh.jinjilive.mobile.ResultEntity;
import com.ggh.jinjilive.mobile.mall.goods.GoodsDetailsMobile;
import com.ggh.jinjilive.mobile.mall.goods.GoodsSpecificationMobile;
import com.ggh.jinjilive.mobile.mall.goods.thegoods.TheGoodsAddressMobile;
import com.ggh.jinjilive.remotedata.InterceptObserver;
import com.ggh.jinjilive.remotedata.systemapi.SystemApiService;
import com.ggh.jinjilive.ui.BaseViewModel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 确认订单
 */
public class AffirmOrderViewModel extends BaseViewModel{
    //获取收货地址
    public MutableLiveData<List<TheGoodsAddressMobile>> TheGoodsAddressMobileList = new MutableLiveData<>();
    //当前选择的地址
    public ObservableField<TheGoodsAddressMobile> mTheGoodsAddressMobile = new ObservableField<>();
    //订单数据
    public MutableLiveData<CommonMobile> mCommonMobile = new MutableLiveData<>();
    //微信付款信息
    public MutableLiveData<WeChatPayMobile> mWeChatPayMobile = new MutableLiveData<>();
    //支付宝付款信息
    public MutableLiveData<String> mAlipayString = new MutableLiveData<>();
    public AffirmOrderViewModel(SystemApiService apiCallInterface) {
        super(apiCallInterface);

    }

    /**
     * 获取收货地址
     */
    public void receiveAddressList(){
        Map<String, Object> map = new HashMap<>();
        addSubscribe(apiCallInterface.receiveAddressList(map).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<List<TheGoodsAddressMobile>>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                List<TheGoodsAddressMobile> theGoodsAddressMobileList = (List<TheGoodsAddressMobile>) resultEntity.getData();
                TheGoodsAddressMobileList.setValue(theGoodsAddressMobileList);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {
                TheGoodsAddressMobileList.setValue(null);
            }
        }));
    }

    /**
     * 添加订单
     */
    public void addOrder(Map<String, Object> map){
        addSubscribe(apiCallInterface.addOrder(map).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<CommonMobile>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                CommonMobile commonMobile = (CommonMobile) resultEntity.getData();
                mCommonMobile.setValue(commonMobile);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {
            }
        }));
    }

    /**
     * 微信付款信息
     * @param map
     */
    public void weChatpayOrder(Map<String, Object> map){
        addSubscribe(apiCallInterface.weChatpayOrder(map).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<WeChatPayMobile>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                WeChatPayMobile weChatPayMobile = (WeChatPayMobile) resultEntity.getData();
                mWeChatPayMobile.setValue(weChatPayMobile);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {
            }
        }));
    }

    /**
     * 支付宝付款信息
     * @param map
     */
    public void alipayPayOrder(Map<String, Object> map){
        addSubscribe(apiCallInterface.alipayPayOrder(map).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<String>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                String alipayString = (String) resultEntity.getData();
                mAlipayString.setValue(alipayString);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {
            }
        }));
    }
}
